<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Follow;
use App\Models\Studying;
class User extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getUserList(){
        $userList = $this->where('status',1)->frist();
        return $userList;
    }

    public function fans($id){
        $follows = Follow::where('user_id',$id)->select('fans_id')->get();
        $fansArr = [];
        foreach($follows as $follow){
            $fansArr[] = $follow->fans_id;
        }
        $users = $this->whereIn('id',$fansArr)->select('openid','id')->get();
        return $users;
    }

    public function getRegisterUser($courseClass,$startTime,$endTime){
        if($startTime !='' && $endTime != ''){
            $userInfo = $this->where('created_at','>=',$startTime)->where('created_at','<=',$endTime)->select('openid','id')->get()->toArray();
            $idArr = array_column($userInfo,'id');
            $studying = Studying::where('course_class_id',$courseClass->id)->whereIn('user_id',$idArr)->select('user_id')->get()->toArray();
            $studyingArr  = array_column($studying,'user_id');
            $idArr = array_diff($idArr,$studyingArr);
            if(!empty($idArr)){
                $userInfos = $this->whereIn('id',$idArr)->select('openid','id')->get();
            }else{
                $userInfos = [];
            }

        }else{
            $userInfos = [];
        }
        return $userInfos;
    }
    public function courseClass(){
        return $this->hasMany('App\Models\CourseClass','user_id');
    }

    public function userAttribute(){
        return $this->belongsTo('App\Models\UserAttribute','id','user_id');
    }
}